package edu.yourl.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import edu.yourl.domain.Favoritable;
import edu.yourl.domain.Gossip;
import edu.yourl.presenter.IHomePagePresenter;
import edu.yourl.web.components.FavoritableResultsPanel;
import edu.yourl.web.components.GossipListPanel;

/**
 * Homepage
 */
public class HomePage extends BasePage implements IHomePage {

	private static final long serialVersionUID = 1L;
	
	@SpringBean
	private IHomePagePresenter presenter;
	
	private String searchFriendsInput;
	private String messageText;
	
	private List<Favoritable> favorites;
	private List<Gossip> gossips;
	
	public HomePage(final PageParameters parameters) {
    	super();
		presenter.init(this);
		
		Form<Void> leaveMessageForm = new Form<Void>("leaveMessageForm");

		final GossipListPanel gossipListPanel = new GossipListPanel("lastGossipsPanel",
				new ListDataProvider<Gossip>(gossips != null ? gossips : new ArrayList<Gossip>()));
		gossipListPanel.setOutputMarkupId(true);

		final TextArea<String> messageTextArea = new TextArea<String>("messageText", new PropertyModel<String>(this, "messageText"));
		messageTextArea.setOutputMarkupId(true);
		leaveMessageForm.add(messageTextArea);
		leaveMessageForm.add(new AjaxSubmitLink("messageSubmit") {
			
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				presenter.addGossip(HomePage.this);
				messageTextArea.clearInput();
				messageText = null;
				target.addComponent(messageTextArea);
				target.addComponent(gossipListPanel);
			}
		});
		leaveMessageForm.add(new FavoritableResultsPanel("favoritesPanel", this.favorites,"No tenes favoritos todavia?"));
		
		
		add(gossipListPanel);
		add(leaveMessageForm);
		
    }


	public String getSearchFriendsInput() {
		return searchFriendsInput;
	}

	public void setSearchFriendsInput(String searchFriends) {
		this.searchFriendsInput = searchFriends;
	}
	
	public String getMessageText() {
		return messageText;
	}


	public void setPresenter(IHomePagePresenter presenter) {
		this.presenter = presenter;
	}

	public void setFavorites(List<Favoritable> favorites) {
		this.favorites = favorites;
	}
	
		@Override
	public void setGossipList(List<Gossip> gossips) {
		this.gossips = gossips;
	}
}
